package com.harwkin.nb.camera.model;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;

import cn.afactory.tygz.model.BaseDataHeader;

/**
 * 图片列表实体类
 */
public class PicsItemList extends BaseDataHeader implements Serializable {
    private static final long serialVersionUID = 3742779964271677600L;

    public ArrayList<PicsItem> list;

    public int totalcount;

    /**
     * 反序列化函数，用于从json字符串反序列化本类型实例
     */
    public static PicsItemList deserialize(String json) throws JSONException {
        if (json != null && !json.isEmpty()) {
            return deserialize(new JSONObject(json));
        }
        return null;
    }

    /**
     * 反序列化函数，用于从json节点对象反序列化本类型实例
     */
    public static PicsItemList deserialize(JSONObject json) throws JSONException {
        if (json != null && json != JSONObject.NULL && json.length() > 0) {
            PicsItemList result = new PicsItemList();

            JSONArray valueArray = json.optJSONArray("data");
            if (valueArray != null) {
                int len = valueArray.length();
                result.list = new ArrayList<PicsItem>(len);
                for (int i = 0; i < len; i++) {
                    JSONObject jo = valueArray.optJSONObject(i);
                    if (jo != null && jo != JSONObject.NULL && json.length() > 0) {
                        result.list.add(PicsItem.deserialize(jo));
                    }
                }
            }

            result.state = json.optInt("state");

            result.message = json.optString("message");

            result.totalcount = json.optInt("totalcount");
            return result;
        }
        return null;
    }

}
